<template>
	<view>
		<view>
			<uni-datetime-picker v-model="checktime" type="date" :clear-icon="false" :end="Date.now()" @change="getClassList"/>
		</view>
	
		<view v-if="roleName === '部门经理'" style="margin: 20rpx; color: white;background-color: #0042f4; height: 250rpx; font-size: 30rpx; line-height: 35px; ">
			<uni-row >
				<uni-col :span="6" >
					<view style=" display: flex; justify-content: center;  align-items: center; line-height: 60px;">每日统计</view>
				</uni-col>
			</uni-row>
			<uni-row >
				<view style="display: flex; justify-content: center;  align-items: center; height: 1rpx; background-color: white;"></view>
			</uni-row>
			<uni-row>
				<uni-col :span="6" >
					<view style=" display: flex; justify-content: center;  align-items: center;">优</view>
				</uni-col>
				<uni-col :span="6" style=" display: flex; justify-content: center;  align-items: center;">
					<view style=" display: flex; justify-content: center;  align-items: center;">良</view>
				</uni-col>
				<uni-col :span="6" style=" display: flex; justify-content: center;  align-items: center;">
					<view style=" display: flex; justify-content: center;  align-items: center;">差</view>
				</uni-col>
				<uni-col :span="6" style=" display: flex; justify-content: center;  align-items: center;">
					<view style=" display: flex; justify-content: center;  align-items: center;">未查</view>
				</uni-col>							
			</uni-row>
			<uni-row>
				<uni-col :span="6" >
					<view style=" display: flex; justify-content: center;  align-items: center;">{{countByCategory.ACount}}</view>
				</uni-col>
				<uni-col :span="6" style=" display: flex; justify-content: center;  align-items: center;">
					<view style=" display: flex; justify-content: center;  align-items: center;">{{countByCategory.BCount}}</view>
				</uni-col>
				<uni-col :span="6" style=" display: flex; justify-content: center;  align-items: center;">
					<view style=" display: flex; justify-content: center;  align-items: center;">{{countByCategory.CCount}}</view>
				</uni-col>
				<uni-col :span="6" style=" display: flex; justify-content: center;  align-items: center;">
					<view style=" display: flex; justify-content: center;  align-items: center;">{{countByCategory.noGradeCount}}</view>
				</uni-col>
			</uni-row>
		</view>
		
		<!--时间段显示-->
		<liuyuno-tabs v-if="hygieneType !== 2" :tabData="checkTimeSpanTabs" 
			@tabClick="checkTimeSpanClick" :activeIndex="selectCheckTimeSpanIndex"></liuyuno-tabs>
		<view class="boxContent" v-for="(item,index) in classList":key="index" @click="popToHygieneDetail(item)">
			<view  class="header">
				<view class="box" style="font-weight: 700; font-size: 25rpx;">{{item.className}}</view>
			</view>
			<view style="width: 1rpx; height: 50rpx; background-color: #f1f1f1;"></view>
			<view class="header">
				<view class="box" style="font-weight: 700; font-size: 25rpx;">{{item.classTeacherName}}</view>
			</view>
			<view style="width: 1rpx; height: 50rpx; background-color: #f1f1f1;"></view>
			<view class="header">
				<view v-if="item.grade === 1" class="box" style="font-weight: 700; font-size: 25rpx;">优</view>
				<view v-if="item.grade === 2" class="box" style="font-weight: 700; font-size: 25rpx;">良</view>
				<view v-if="item.grade === 3" class="box" style="font-weight: 700; font-size: 25rpx;">差</view>
				<view v-if="item.grade == null || item.grade === ''" class="box" style="font-weight: 700; font-size: 25rpx;">未查</view>
			</view>
		</view>
		<view class="pic" v-if="classList.length <= 0">
			<image src="https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/wujilu.png" mode="">
			</image>
		</view>
	</view>
</template>

<script>
	import classManage from "@/api/modules/classManage.js"
	import { parseTime } from "@/utils/date.js"
	export default {
		data() {
			return {
				roleName: '',
				classList: [],
				hygieneType: '',
				checkTimeSpanTabs : ["上午","下午"], //检查时间段tab
				selectCheckTimeSpan : 0, //选中时间段
				selectCheckTimeSpanIndex: 0, //选中时间段下标
				countByCategory: {
					ACount: 0,
					BCount: 0,
					CCount: 0,
					noGradeCount: 0,
				},
				checktime: parseTime(Date.now()).slice(0, 10),
			}
		},
		methods: {
			//时间段点击
			checkTimeSpanClick(index) {
				if(index === 0) {
					this.hygieneType = 0; //上午
				}else { 
					this.hygieneType = 1; //下午
				}
				
				this.getClassList();
			
			},
			popToHygieneDetail(e) {
				console.log("popToViolationDetail",e)
				uni.navigateTo({
					url: `/pages_duty/deptManager/classroomHygiene/classHygieneDetail?classHygieneId=${e.classHygieneId}&hygieneType=${this.hygieneType}&classId=${e.classId}&checktime=${this.checktime}`,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			getClassList() {
				uni.showLoading({
					mask: true,
					title: '加载中'
				});
				classManage.getAppClassHygieneList(this.hygieneType, this.checktime).then(resp => {
					this.classList = resp.data
					uni.hideLoading();
					
					const typeMapping = {
					  1: 'ACount',
					  2: 'BCount',
					  3: 'CCount',
					  null: 'noGradeCount'
					};
					const expectedTypes = ['ACount', 'BCount', 'CCount', 'noGradeCount']; // 预期的属性值列表
					const countByType = {};
					for (const item of this.classList) {
					  const newType = typeMapping[item.grade]; // 获取新的数字字段名称
					
					  if (countByType[newType]) {
					    countByType[newType]++;
					  } else {
					    countByType[newType] = 1;
					  }
					}
					
					expectedTypes.forEach((type) => {
					  if (!countByType[type]) {
					    countByType[type] = 0;
					  }
					});
					
					this.countByCategory = countByType
					console.log(countByType);
				})
			}
		},
		onLoad(options) {
			this.roleName = uni.getStorageSync("roleName");
			this.hygieneType = parseInt(options.hygieneType, 10);
			if(this.hygieneType !== 2) {
				if(new Date().getHours()>=12) { //下午
					this.hygieneType = 1
					this.selectCheckTimeSpanIndex = 1
				}else { //上午
					this.hygieneType = 0
					this.selectCheckTimeSpanIndex =0
				}
			}
			
		},
		onShow() {
			this.getClassList();
		}
	}
</script>

<style>
	.boxContent {
		background-color: #fff;
		border-radius: 20rpx;
		margin: 10rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx;
		text-align: center;
		border: 1px solid #a2a2a254;
		/* margin-top: 10rpx; */
	}
	
	.header {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		height: 40rpx;
		width: 50%;
	}
</style>
